package com.gtt.lazy.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.lang.StringUtils;

import com.gtt.lazy.domain.User;
import com.gtt.lazy.service.AgentService;
import com.gtt.lazy.service.CategoryService;
import com.gtt.lazy.service.ColorService;
import com.gtt.lazy.service.NoticeService;
import com.gtt.lazy.service.SofaService;
import com.gtt.lazy.service.UserService;

/**
 * 登录Action
 * 
 * @author 高甜甜
 * @date 2009-7-9
 */
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private static final String[] DAYS = new String[] { "星期日", "星期一", "星期二",
			"星期三", "星期四", "星期五", "星期六" };

	private CategoryService categoryService;
	private SofaService sofaService;
	private AgentService agentService;
	private NoticeService noticeService;
	private UserService userService;
	private ColorService colorService;

	private String username;
	private String password;
	private String date;
	private int countCat;
	private int countSofa;
	private int countAgent;
	private int countNotice;
	private String oldPass;
	private String newPass;

	private int countColors;

	@SuppressWarnings("unchecked")
	public String login() {
		if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
			if (userService.exist(new User(username, password))) {
				getSession().put(Constant.USERNAME, username);
				return SUCCESS;
			}
		}
		addActionError(Constant.ERROR_LOGIN);
		return ERROR;
	}

	public String toModPass() {
		return SUCCESS;
	}

	public String modPass() {
		if (userService.exist(new User("admin", oldPass))) {
			User user = userService.findByUsername("admin");
			user.setPassword(newPass);
			userService.update(user);
			addActionError(Constant.SUC_MOD_PASS);
			return SUCCESS;
		}
		addActionError(Constant.ERROR_OLD_PASS);
		return ERROR;
	}

	public String logout() {
		getSession().remove(Constant.USERNAME);
		return SUCCESS;
	}

	public String welcome() {
		date = getStringDate();
		countCat = categoryService.count();
		countSofa = sofaService.count();
		countAgent = agentService.count();
		countNotice = noticeService.count();
		countColors = colorService.count();
		return SUCCESS;
	}

	private String getStringDate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime()) + " "
				+ DAYS[cal.get(Calendar.DAY_OF_WEEK) - 1];
	}

	public String getOldPass() {
		return oldPass;
	}

	public void setOldPass(String oldPass) {
		this.oldPass = oldPass;
	}

	public String getNewPass() {
		return newPass;
	}

	public void setNewPass(String newPass) {
		this.newPass = newPass;
	}

	public int getCountCat() {
		return countCat;
	}

	public void setCountCat(int countCat) {
		this.countCat = countCat;
	}

	public int getCountSofa() {
		return countSofa;
	}

	public void setCountSofa(int countSofa) {
		this.countSofa = countSofa;
	}

	public int getCountAgent() {
		return countAgent;
	}

	public void setCountAgent(int countAgent) {
		this.countAgent = countAgent;
	}

	public int getCountNotice() {
		return countNotice;
	}

	public void setCountNotice(int countNotice) {
		this.countNotice = countNotice;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public SofaService getSofaService() {
		return sofaService;
	}

	public void setSofaService(SofaService sofaService) {
		this.sofaService = sofaService;
	}

	public AgentService getAgentService() {
		return agentService;
	}

	public void setAgentService(AgentService agentService) {
		this.agentService = agentService;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ColorService getColorService() {
		return colorService;
	}

	public void setColorService(ColorService colorService) {
		this.colorService = colorService;
	}

	public int getCountColors() {
		return countColors;
	}

	public void setCountColors(int countColors) {
		this.countColors = countColors;
	}

}
